/*
 * This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.sf.odinms.net.channel.handler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleStat;
import net.sf.odinms.provider.MapleData;
import net.sf.odinms.provider.MapleDataProvider;
import net.sf.odinms.provider.MapleDataProviderFactory;
import net.sf.odinms.provider.MapleDataTool;
//import net.sf.odinms.tools.StringUtil;
import net.sf.odinms.server.TimerManager;
import net.sf.odinms.server.maps.MapleMap;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.Pair;

/**
 *
 * @author Bassoe
 */
public class OXHandler {
    private static MapleDataProvider stringData = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Etc.wz"));
    private ScheduledFuture<?> oxstarted;
    private int cur = 0;
    private int dir = 1;
	
    public static String getOXQuestion(int imgdir, int id){
        List<Pair<Integer, String>> itemPairs = new ArrayList<Pair<Integer, String>>();
        MapleData itemsData = stringData.getData("OXQuiz.img").getChildByPath("" + imgdir + "");
        MapleData itemFolder = itemsData.getChildByPath("" + id + "");
        int itemId = Integer.parseInt(itemFolder.getName());
        String itemName = MapleDataTool.getString("q", itemFolder, "NO-NAME");
        itemPairs.add(new Pair<Integer, String>(itemId, itemName));
        return itemPairs.toString();
    }

    public static int getOXAnswer(int imgdir, int id){
        MapleData itemsData = stringData.getData("OXQuiz.img").getChildByPath("" + imgdir + "");
        MapleData itemFolder = itemsData.getChildByPath("" + id + "");
        int bla = MapleDataTool.getInt(itemFolder.getChildByPath("a"));
        return bla;
    }

    public static String getOXExplain(int imgdir, int id){
        List<Pair<Integer, String>> itemPairs = new ArrayList<Pair<Integer, String>>();
        MapleData itemsData = stringData.getData("OXQuiz.img").getChildByPath("" + imgdir + "");
        MapleData itemFolder = itemsData.getChildByPath("" + id + "");
        int itemId = Integer.parseInt(itemFolder.getName());
        String itemName = MapleDataTool.getString("d", itemFolder, "NO-NAME");
        itemPairs.add(new Pair<Integer, String>(itemId, itemName));
        return itemPairs.toString();
    }

    public void startOXQuiz(final MapleMap map) {
        map.broadcastMessage(MaplePacketCreator.getClock(25));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "Hello and welcome to the OX Quiz"));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "The rules of OX Quiz are very simple"));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "There will appear questions. If true, walk to the O, else walk to the X"));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "If you answer a question incorrect you will die"));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "The last remainer will win the OX Quiz"));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "."));
        map.broadcastMessage(MaplePacketCreator.serverNotice(6, "Now have fun, and good luck!"));
        ScheduledFuture<?> schedule = TimerManager.getInstance().register(new Runnable() {
            @Override
            public void run() {
                cur++;
                try {
                    map.broadcastMessage(MaplePacketCreator.serverNotice(6, "[OX] " + getOXQuestion(dir, cur)));
                } catch (NullPointerException e) {
                    cur = 1;
                    dir++;
                    map.broadcastMessage(MaplePacketCreator.serverNotice(6, "[OX] " + getOXQuestion(dir, cur)));
                }
                map.broadcastMessage(MaplePacketCreator.getClock(20));

                TimerManager.getInstance().schedule(new Runnable() {
                    @Override
                    public void run() {
                        boolean kill = false;
                        map.broadcastMessage(MaplePacketCreator.serverNotice(5, "[OX] " + getOXExplain(dir, cur)));
                        map.broadcastMessage(MaplePacketCreator.getClock(5));
                        for (MapleCharacter player : map.getCharacters()) {
                            if (player != null) {
                                if (!player.isGM()) {
                                    switch (getOXAnswer(dir, cur)) {
                                        case 0:
                                            if (player.getPosition().x < -143) kill = true;
                                            break;
                                        case 1:
                                            if (player.getPosition().x > -308) kill = true;
                                            break;
                                        default:
                                            break;
                                    }
                                    if (player.getPosition().y <= -206 || (player.getPosition().x > -308 && player.getPosition().x < -143)) kill = true;
                                    if (kill) {
                                        player.setHp(0);
                                        player.updateSingleStat(MapleStat.HP, 0);
                                        player.dropMessage("Sorry, but you weren't on the right position");
                                    }
                                }
                            }
                        }
                    }
                }, 20000);
            }
        }, 25000, 25000);
        oxstarted = schedule;
    }

    public void stopOX() {
        oxstarted.cancel(true);
    }
}